package leetcode;

import java.util.LinkedList;

public class EvaluateReversePolishNotation {
    public int evalRPN(String[] tokens) {
        LinkedList<Integer> stack = new LinkedList();
        for (String token : tokens) {
            switch (token) {
                case "+":
                    Integer a = stack.pop();
                    Integer b = stack.pop();
                    stack.push(b + a);
                    break;
                case "-":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b - a);
                    break;
                case "*":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b * a);
                    break;
                case "/":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b / a);
                    break;
                default:
                    stack.push(Integer.parseInt(token));
                    break;
            }
        }
        return stack.pop();
    }
    public static void main(String[] args) {
        String[] a = {"4", "13", "5", "/", "+"};
        EvaluateReversePolishNotation e = new EvaluateReversePolishNotation();
        e.evalRPN(a);
    }
}
